package moltencore.game.collision;

import moltencore.game.bubble.AbstractBubble;
import spark.math.Vector2;
import moltencore.game.bubble.Bubble;

public class VerticalLine implements ICollidable {
	private static final int COLLISION_ACCURACY = 5;

	private boolean sticky;
	private float xPosition;

	public VerticalLine(float xPosition, boolean sticky) {
		this.xPosition = xPosition;
		this.sticky = sticky;
	}

	@Override
	public Vector2 collisionPoint(AbstractBubble bubble, Vector2 movement) {
		Vector2 newPosition = Vector2.add(bubble.getPosition(), movement);
		if(!collides(newPosition, Bubble.COLLISION_RADIUS)) {
			return null;
		}
		if(movement.x < 0) {
			newPosition.x = xPosition + Bubble.COLLISION_RADIUS;
		}
		else {
			newPosition.x = xPosition - Bubble.COLLISION_RADIUS;
		}
		return newPosition;
	}

	@Override
	public boolean collides(AbstractBubble bubble, Vector2 movement) {
		Vector2 newPosition = (movement == null) ? bubble.getPosition() : Vector2.add(bubble.getPosition(), movement);
		return collides(newPosition, Bubble.COLLISION_RADIUS);
	}

	private boolean collides(Vector2 circlePosition, float circleRadius) {
		float circleX = circlePosition.x;
		//fix the x coordinate of the bubble so that it is right of the line
		if(circleX < xPosition) {
			circleX += 2 * (xPosition - circleX);
		}
		//if the left edge of the bubble is left of this line there is a collision
		return (circleX - circleRadius) < xPosition;
	}

	@Override
	public void collisionResponse(AbstractBubble bubble) {
		// if this collidable is sticky the bubble will stick to it and not move
		if(isSticky()) {
			bubble.getVelocity().x = 0;
			bubble.getVelocity().y = 0;
		}
		// else the bubble is reflected
		else {
			bubble.getVelocity().x *= -1;
		}
	}

	@Override
	public boolean isSticky() {
		return sticky;
	}

	@Override
	public boolean isPartOfWorld() {
		return true;
	}

	public float getXPosition() {
		return xPosition;
	}
}
